﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.vehicle.maintenance
{
    public partial class query : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new VehicleDao().SelectAll(base.Conn);
                this.ddlVehicleId.DataSource = table;
                this.ddlVehicleId.DataValueField = "VehicleId";
                this.ddlVehicleId.DataTextField = "Number";
                this.ddlVehicleId.DataBind();
                this.ddlVehicleId.Items.Insert(0, new ListItem("", ""));
                this.txtBeginDate.Text = string.Concat(new object[] { DateTime.Now.Year, "-", DateTime.Now.Month.ToString(), "-1" });
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                StringBuilder builder = new StringBuilder();
                ArrayList list = new ArrayList();
                builder.Append("SELECT [Vehicle_Maintenance].*,[Vehicle].[Number] FROM [dbo].[Vehicle],[dbo].[Vehicle_Maintenance] WHERE [Vehicle_Maintenance].[VehicleId] = [Vehicle].[VehicleId] ");
                if (this.ddlVehicleId.SelectedValue != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[VehicleId] = @VehicleId ");
                    list.Add(new object[] { "@VehicleId", Convert.ToInt32(this.ddlVehicleId.SelectedValue) });
                }
                if (this.txtBeginDate.Text != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[RequestDate] >= @BeginDate ");
                    list.Add(new object[] { "@BeginDate", Convert.ToDateTime(this.txtBeginDate.Text + " 00:00:00") });
                }
                if (this.txtEndDate.Text != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[RequestDate] <= @EndDate ");
                    list.Add(new object[] { "@EndDate", Convert.ToDateTime(this.txtEndDate.Text + " 23:59:59") });
                }
                if (this.ddlType.SelectedValue != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[Type] = @Type ");
                    list.Add(new object[] { "@Type", this.ddlType.SelectedValue });
                }
                if (this.txtReason.Text != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[Reason] LIKE '%' + @Reason + '%' ");
                    list.Add(new object[] { "@Reason", this.txtReason.Text });
                }
                if (this.txtPerson.Text != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[Person] LIKE '%' + @Person + '%' ");
                    list.Add(new object[] { "@Person", this.txtPerson.Text });
                }
                if (this.txtFeeMin.Text != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[Fee] >= @FeeMin ");
                    list.Add(new object[] { "@FeeMin", Convert.ToDecimal(this.txtFeeMin.Text) });
                }
                if (this.txtFeeMax.Text != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[Fee] <= @FeeMax ");
                    list.Add(new object[] { "@FeeMax", Convert.ToDecimal(this.txtFeeMax.Text) });
                }
                if (this.txtRemark.Text != string.Empty)
                {
                    builder.Append(" AND [Vehicle_Maintenance].[Remark] LIKE '%' + @Remark + '%' ");
                    list.Add(new object[] { "@Remark", this.txtRemark.Text });
                }
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    object[] objArray11 = (object[])list[i];
                    commandParameters[i] = new SqlParameter(objArray11[0].ToString(), objArray11[1]);
                }
                DataSet set = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, builder.ToString(), commandParameters);
                set.Tables[0].TableName = "Vehicle_Maintenance";
                DataTable table2 = set.Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.pnlSearch.Visible = true;
                    this.rptSearch.DataSource = table2;
                    this.rptSearch.DataBind();
                    decimal num3 = 0.0M;
                    for (int j = 0; j < table2.Rows.Count; j++)
                    {
                        if (table2.Rows[j]["Fee"] != DBNull.Value)
                        {
                            num3 += Convert.ToDecimal(table2.Rows[j]["Fee"].ToString());
                        }
                    }
                    this.litTotalFee.Text = num3.ToString();
                }
                else
                {
                    this.litMessage.Text = base.ShowPrompt("暂无车辆维护信息", "query.aspx");
                }
            }
        }

    }
}
